import pool from '../config/database'
import { RowDataPacket } from 'mysql2'

export interface User extends RowDataPacket {
  id: number
  username: string
  password: string
  nickname: string
  avatar: string
  role: 'admin' | 'user'
  status: number
}

export interface CreateUserData {
  username: string
  password: string
  nickname: string
  role: 'admin' | 'user'
}

export class UserModel {
  static async findByUsername(username: string): Promise<User | null> {
    const [rows] = await pool.execute<User[]>(
      'SELECT * FROM users WHERE username = ?',
      [username]
    )
    return rows[0] || null
  }

  static async create(userData: CreateUserData) {
    const [result] = await pool.execute(
      'INSERT INTO users (username, password, nickname, role) VALUES (?, ?, ?, ?)',
      [userData.username, userData.password, userData.nickname, userData.role]
    )
    return result
  }
} 